//
//  UIImageView-Extension.swift
//  Whiteboard
//
//  Created by point on 2018/1/10.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import Kingfisher

extension  UIImageView {
    
    
    /// 快速创建imageView
    convenience init(imgName:String ){
        self.init()
        self.image = UIImage(named: imgName)
    }
    
    
    /// 显示网络图片 : 缩略图
    func showImageS(_ url:String)  {
        let myurl  = url + "/s"
        if let codeUrlStr = myurl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ,let codeUrl = URL(string: codeUrlStr) {
            self.kf.setImage(with: ImageResource.init(downloadURL: codeUrl), placeholder:  UIImage(named: "pic_light"))
            self.contentMode = .scaleAspectFill
        }else{
            self.image = UIImage(named: "pic_light")
        }
    }
    
    
    /// 显示网络图片 : 完全显示
    func showImageI(_ url:String)  {
        let myurl  = url + "/i"
        if let codeUrlStr = myurl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ,let codeUrl = URL(string: codeUrlStr) {
            self.kf.setImage(with: ImageResource.init(downloadURL: codeUrl), placeholder:  UIImage(named: "pic_light"))
            self.contentMode = .scaleAspectFill
        }else{
            self.image = UIImage(named: "pic_light")
        }
    }
    
    
//    func showImageB(url:String , _ size:CGSize = CGSize.zero)  {
//        var myurl  = ""
//        if size == CGSize.zero {
//            myurl = url
//        }else {
//            myurl = url + "?imageView2/2/w/\(Int(size.width) * 2 )/h/\(Int(size.height) * 2)"
//        }
//
//        if let codeUrlStr = myurl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ,let codeUrl = URL(string: codeUrlStr) {
//            self.kf.setImage(with: ImageResource.init(downloadURL: codeUrl), placeholder:  UIImage(named: "pic_light"))
//            self.contentMode = .scaleAspectFill
//        }else{
//            self.image = UIImage(named: "pic_light")
//        }
//    }
    
    
//    /// 显示网络图片 : 缩略图
//    func showImage(url:String , _ size:CGSize = CGSize.zero)  {
//        var myurl  = ""
//        if size == CGSize.zero {
//            myurl = url
//        }else {
//            myurl = url + "?imageView2/2/w/\(Int(size.width) * 2 )/h/\(Int(size.height) * 2)"
//        }
//
//        if let codeUrlStr = myurl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ,let codeUrl = URL(string: codeUrlStr) {
//            self.kf.setImage(with: ImageResource.init(downloadURL: codeUrl), placeholder:  UIImage(named: "pic_light"))
//            self.contentMode = .scaleAspectFill
//        }else{
//            self.image = UIImage(named: "pic_light")
//        }
//    }
    
    
//    /// 裁剪网络图片
//    func showImageG(url:String)  {
//        self.backgroundColor = UIColor.black.withAlphaComponent(0.1)
//        self.image =  UIImage.emptyImage(imageSize: CGSize(width: 50, height: 50))
//        self.contentMode = .center
//        
//        if url.count > 0 {
//            let iconURL = URL(string: url)!
//            // self.kf.setImage(with: iconURL, placeholder: UIImage.emptyImage(imageSize: CGSize(width: 50, height: 50)))
//            //.scaleAspectFit 等比例缩放
//            //.scaleToFill 完全填充 变形
//            //.scaleAspectFill 裁剪填充
//            
//            self.kf.setImage(with: iconURL,
//                             placeholder:  UIImage.emptyImage(imageSize: CGSize(width: 50, height: 50)),
//                             options: nil,//[.transition(.fade(3))],
//                             progressBlock: { receivedSize, totalSize in
//                                
//            },
//                             completionHandler: { image, error, cacheType, imageURL in
//                                self.contentMode = .scaleAspectFit
//            })
//            
//        }
//    }
}
